<template>
  <div>
    <a-form-model ref="ruleForm" layout="vertical" :model="form" :rules="rules">
      <a-form-model-item label="名称" prop="formName">
        <a-input v-model="form.formName" placeholder="请输入名称" />
      </a-form-model-item>
      <a-form-model-item label="所在分组" prop="categoryId">
       <category-select v-model="form.categoryId"></category-select>
      </a-form-model-item>
      <a-form-model-item label="表单说明" prop="remark">
        <a-textarea v-model="form.remark"></a-textarea>
      </a-form-model-item>
      <a-form-model-item label="谁可以发起提交" prop="remark">
        <a-select/>
      </a-form-model-item>
      <a-form-model-item label="谁可以管理这个模板并导出数据" prop="remark">
        <a-select/>
      </a-form-model-item>
      <a-form-model-item label="谁可以查看所有数据" prop="remark">
        <a-select/>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>
<script>
import { save } from '@/api/category'
import CategorySelect from '../../category/components/CategorySelect'
export default {
  name: 'BaseForm',
  components: { CategorySelect },
  props: {
    value: Object
  },
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      form: Object.assign({
        id: '',
        formName: '',
        categoryId: 1,
        remark:''
      }, this.value),
      rules: {
        formName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        categoryId: [{ required: true, message: '请选择所在分组', trigger: 'change' }],
        ssoRedirectUri: [{ required: true, message: '请输入SSO跳转地址', trigger: 'change' }]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          save(this.form).then(res => {
            res.success ? this.$message.success('保存成功') : this.$message.error('保存失败')
            this.$emit('done', res.success)
          }).catch(res => {
            this.$emit('done', res.success)
          })
        } else {
          this.$emit('done', false)
          return false
        }
      })
    },
    resetForm() {
      // 重置表单数据
      this.$refs.ruleForm.resetFields()
      this.$utils.merge(this.form, this.$options.data()['form'])
    },
    loadData(data) {
      if (data) {
        this.$utils.merge(this.form, data)
      }
    }
  }
}
</script>
